/*
 * web.xml:
 * <listener>
 * 	<listener-class>fully-qualified-class-name</listener-class>
 * </listener>
 */
package btanalyzer.downloader.web;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import btanalyzer.downloader.config.SpringContextManager;

/**
 * Starts and stops Spring application context with web application context.
 * @author pawel
 *
 */
public class SpringContextManagingListener implements ServletContextListener {
	
	private static final Logger log = LoggerFactory.getLogger(SpringContextManagingListener.class);
	
	private static SpringContextManager springContextManager = null;

	public void contextInitialized(ServletContextEvent arg0) {
		
		log.info("Starting btanalyzer-downloader app");
		
		try {
			springContextManager = new SpringContextManager();
			springContextManager.start();
		} catch (Exception e) {
			log.error("Error while starting btanalyzer-downloader app" ,e);
		}
	}

	public void contextDestroyed(ServletContextEvent arg0) {

		log.info("Stopping btanalyzer-downloader app");
		
		try {
			springContextManager.stop();
		} catch (Exception e) {
			log.error("Error while stopping btanalyzer-downloader app" ,e);
		}
	}

}
